package com.space.user.event;

import com.space.client.domainevent.CreateUserAccountEvent;
import com.space.user.domain.account.service.UserAccountDomainService;
import com.space.user.domain.account.valueobject.UserId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * @author <a href="mailto:414563035@qq.com">Bedroom</a>
 * @date 2021/4/28 11:23 上午
 */
@Configuration
@Slf4j
public class EventReceptor implements CreateUserAccountEvent.Consumer {
    @Autowired
    UserAccountDomainService userAccountDomainService;

    @Bean
    @Override
    public Function<Flux<Message<CreateUserAccountEvent>>, Mono<Void>> createUserAccountEvent() {
        return flux -> flux.map(message -> {
            final CreateUserAccountEvent event = message.getPayload();
            log.debug("接收到了事件: {}", event);
            userAccountDomainService.createAccountInfo(
                    new UserId(event.getUserId())
            );
            return message;
        }).then();
    }
}


